﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using ExtBusiness;
using ExtBusiness.Common;

public partial class Office_ExtFileUpload : System.Web.UI.Page
{
    UploadFileClassNew ufcGloab = new UploadFileClassNew();

    /// <summary>
    /// 呈现前的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_PreRender(object sender, EventArgs e)
    {
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SetFileUpdatePerproty();
        DeleteResource();

        if (!IsPostBack)
        {
            BindData();

            if (!string.IsNullOrEmpty(Request["step"]))
            {
                //判断是否是上传岗位
                if (!SystemConfig.SpecialStepName.Contains(Request["step"]))
                {
                    FileUpdate1.Visible = false;

                    CustomGridView1.Columns[5].Visible = false;
                }
            }
        }
    }

    /// <summary>
    /// 删除附件
    /// </summary>
    private void DeleteResource()
    {
        if (!string.IsNullOrEmpty(Request["id"]) && Request["action"].CompareTo("delete") == 0)
        {
            ufcGloab.DeleteStoreFile(FileUpdate1.TableName,FileUpdate1.KeyId, Request["id"]);
        }
    }

    /// <summary>
    /// 设置文件上传属性
    /// </summary>
    private void SetFileUpdatePerproty()
    {
        //设置文件上传控件
        FileUpdate1.KeyId = "id";
        FileUpdate1.FileStoreFieldName = "FILEDATA";
        FileUpdate1.FileTypeFieldName = "FILETYPE";
        FileUpdate1.TableName = "XT_SERIAL_ATTACHMENT";

        FileUpdate1.StoreKeyFieldName = "AppDataId";
        FileUpdate1.StoreParamFieldName = "MODULE_ID";
        if (!string.IsNullOrEmpty(Request["iid"]))
        {
            FileUpdate1.StoreKeyValue = Request["iid"];
            //FileUpdate1.StoreParamValue = ddlZylb.ID;
        }

        FileUpdate1.StoreResourceFieldName = "FILENAME";        
        //FileUpdate1.StoreResourceNameControlName = txtResourceName.ID;
        FileUpdate1.StoreResourceType = "insert";
    }

    /// <summary>
    /// 上传后触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void FileUpdate1_AfterUploadedEvent(object sender, EventArgs e)
    {
        //绑定数据
        BindData();
    }

    /// <summary>
    /// 绑定数据源,绑定datagrid
    /// </summary>
    private void BindData()
    {
        string strWhere = string.Format(" AppDataId='{0}'", Request["iid"]);
        DataTable dtSource = ufcGloab.GetAttachmentResourceInfo(strWhere);
        CustomGridView1.DataSource = dtSource;

        CustomGridView1.PageSize = SystemConfig.PageSize;
        CustomGridView1.RecordCount = dtSource.Rows.Count;
        CustomGridView1.DataBind();
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
    {
        BindData();
    }

    /// <summary>
    /// 行创建
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCellCollection dataTC = e.Row.Cells;

            HyperLink hlTemp = e.Row.FindControl("hlDelete") as HyperLink;

            if (hlTemp != null)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;

                if (dtr != null)
                {
                    hlTemp.NavigateUrl = string.Format("ExtFileUpload.aspx?id={0}&iid={1}&action=delete",
                        dtr["id"], Request["iid"]);
                }
            }
        }
    }
}
